package com.mingchuang.administrator.mingchuang;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mingchuang.administrator.mingchuang.utils.Constant;

import java.io.File;

/**
 * Created by Administrator on 2016/1/13.
 */
public class FileLoadActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("文件上传");
        actionBar.show();
    }


    private static void  upload(){

        RequestParams params = new RequestParams();
        //params.addHeader("name", "value");
        //params.addQueryStringParameter("name", "value");

        // 只包含字符串参数时默认使用BodyParamsEntity，
        // 类似于UrlEncodedFormEntity（"application/x-www-form-urlencoded"）。
        //params.addBodyParameter("name", "value");

        // 加入文件参数后默认使用MultipartEntity（"multipart/form-data"），
        // 如需"multipart/related"，xUtils中提供的MultipartEntity支持设置subType为"related"。
        // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity（如：
        // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity）。
        // 例如发送json参数：params.setBodyEntity(new StringEntity(jsonStr,charset));

        String path = Constant.getPath()+"/received/Y12_4.7z";
        params.addBodyParameter("file", new File(path));
        HttpUtils http =  new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, "http://116.255.189.224:8080/jfinal_authority_mysql/upload/upload", params, new RequestCallBack<String>() {


            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.d("kipeng","onSuccess"+responseInfo.toString());


            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.d("kipeng","onFailure:"+s+"/"+e.getMessage());

            }
        });





    }

}
